import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import Router from "vue-router";
import store from "./store";
import * as echarts from "echarts";
import "echarts-gl";
import "./static/style/base.css";
import "font-awesome/css/font-awesome.css";
//自定义css

//引入element-ui
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
//引入自定义axios方法
import {
  postRequest,
  putRequest,
  getRequest,
  deleteRequest
} from "./utils/api";
//全局路由
import { initMenu } from "./utils/menus";
//下载download
import { downloadRequest } from "./utils/download";
//插件形式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;
Vue.prototype.$echarts = echarts;

//全局路由导航守卫
router.beforeEach((to, from, next) => {
  // 是否登录
  if (window.sessionStorage.getItem("tokenStr")) {
    initMenu(router, store);

    // 判断用户信息是否存在
    if (!window.sessionStorage.getItem("user")) {
      return getRequest("/admin/info").then(resp => {
        if (resp) {
          // 存入用户信息
          window.sessionStorage.setItem("user", JSON.stringify(resp));
          //此处path为login中方法传来的值
          next(to.path);
        }
      });
    }
    next();
  } else {
    if (to.path === "/") {
      next();
    } else {
      //权限不足时跳转登录页，但通过redirect保存了要去的路径
      next("/?redirect=" + to.path);
    }
  }
});

//vuerouter3.0跳转问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.config.productionTip = false;
Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
